// Copyright 2017 Baidu Inc. All Rights Reserved.
// Author: Su Hao (suhao@baidu.com)
//
// Description: Adapter between nsdl and Baidu CA CoAP.

#include "baidu_ca_nsdl_adapter.h"
#include "sn_nsdl_lib.h"
#if MBED_CLIENT_C_VERSION > 29999
#include "sn_coap_protocol_internal.h"
#endif

BCA_INT_IMPL void bca_nsdl_address_set(sn_nsdl_addr_s* target,
                                       const bca_addr_t* source) {
    if (target && source) {
        target->addr_len = source->host_size;
        target->addr_ptr = source->host;
        target->port = source->port;
        target->type = SN_NSDL_ADDRESS_TYPE_IPV4;
    }
}

BCA_INT_IMPL void bca_nsdl_header_set(sn_coap_hdr_s* target,
                                      sn_coap_options_list_s* opt,
                                      const bca_msg_t* source) {
    if (target && source) {
        BCA_MEMSET(target, 0, sizeof(sn_coap_hdr_s));
        target->msg_code = (sn_coap_msg_code_e)source->msg_code;
        target->msg_id = source->msg_id;
        target->msg_type = (sn_coap_msg_type_e)source->msg_type;
        target->payload_len = source->payload_len;
        target->payload_ptr = source->payload;
        target->token_len = source->token_len;
        target->token_ptr = (uint8_t*)source->token;
        target->uri_path_len = source->path_len;
        target->uri_path_ptr = source->path;
#if MBED_CLIENT_C_VERSION > 29999
        target->content_format = COAP_CT_NONE;
#endif

        if (opt && source->query != NULL && source->query_len > 0) {
            BCA_MEMSET(opt, 0, sizeof(sn_coap_options_list_s));
#if MBED_CLIENT_C_VERSION > 29999
            opt->max_age = COAP_OPTION_MAX_AGE_DEFAULT;
            opt->uri_port = COAP_OPTION_URI_PORT_NONE;
            opt->observe = COAP_OBSERVE_NONE;
            opt->accept = COAP_CT_NONE;
            opt->block2 = COAP_OPTION_BLOCK_NONE;
            opt->block1 = COAP_OPTION_BLOCK_NONE;
#endif
            opt->uri_query_len = source->query_len;
            opt->uri_query_ptr = source->query;
            target->options_list_ptr = opt;
        }
    }
}

BCA_INT void bca_coap_address_set(bca_addr_t* target,
                                  const sn_nsdl_addr_s* source) {
    if (target && source) {
        target->host_size = source->addr_len;
        target->host = source->addr_ptr;
        target->port = source->port;
        target->type = (bca_u8_t) - 1;
    }
}

BCA_INT void bca_coap_header_set(bca_msg_t* target,
                                 const sn_coap_hdr_s* source) {
    if (target && source) {
        BCA_MEMSET(target, 0, sizeof(bca_msg_t));
        target->msg_code = source->msg_code;
        target->msg_id = source->msg_id;
        target->msg_type = source->msg_type;
        target->payload_len = source->payload_len;
        target->payload = source->payload_ptr;
        target->token = source->token_ptr;
        target->token_len = source->token_len;
        target->path_len = source->uri_path_len;
        target->path = source->uri_path_ptr;
        sn_coap_options_list_s* opt = source->options_list_ptr;

        if (opt != NULL && opt->uri_query_ptr != NULL && opt->uri_query_len > 0) {
            target->query_len = opt->uri_query_len;
            target->query = opt->uri_query_ptr;
        }
    }
}
